package la.iok.hzsvn.lewin.movie.operation.timer;

import la.iok.hzsvn.lewin.movie.operation.service.LuckyMoneyService;
import la.iok.hzsvn.lewin.movie.operation.service.PlayRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.LinkedList;
import java.util.List;

/**
 * 返利定时任务服务
 */
public class ReturnMoneyScheduleService {
    private static final Logger logger = LoggerFactory.getLogger(ReturnMoneyScheduleService.class);
    private final PlayRecordService playRecordService;
    private final LuckyMoneyService luckyMoneyService;

    public ReturnMoneyScheduleService(PlayRecordService playRecordService,
                                      LuckyMoneyService luckyMoneyService) {
        this.playRecordService = playRecordService;
        this.luckyMoneyService = luckyMoneyService;
    }

    @Scheduled(cron = "2 1 0 1 * *")
    public void doReturnMoney(){
        logger.debug("执行返利任务");
        if(!luckyMoneyService.switchStatus()){
            logger.info("返利开关关闭,定时任务未执行任何操作,立即结束");
            return;
        }
        // 失败的账号
        List<Long> excludes = new LinkedList<>();
        // 成功发放返利的账号
        List<Long> success = new LinkedList<>();
        // 开本记录不达标的账号
        List<Long> lessAmount = new LinkedList<>();
        try{
            playRecordService.processReturnMoney(excludes,success,lessAmount);
        }catch (Exception e){
            logger.error("执行返利任务异常",e);
        }
        logger.info("本次成功处理的记录:{},失败的记录:{},不达标的记录:{}",success,excludes,lessAmount);
    }
}
